/**
 * @description: 返回数组中次数最多的数字
 * @author: huen2015
 */

function getMaxCountByArr(arr: number[] = []): number[] {
	if (!arr.length) return []

	let curMaxCount = 0
	let maxCount = 0
	let result: number[] = []

	const tempArr = arr.sort((a: number, b: number) => a - b)
	console.log('tempArr', tempArr)
	tempArr.forEach((item: number, index: number) => {
		if (index === 0) {
			curMaxCount = 1
			result = [item]
			maxCount = 1
		} else {
			if (tempArr[index - 1] !== item) {
				if (curMaxCount > maxCount) {
					maxCount = curMaxCount
					result = [tempArr[index - 1]]
				} else if (curMaxCount === maxCount) {
					result?.push(tempArr[index - 1])
				}
				curMaxCount = 1
			} else {
				curMaxCount++
			}
		}
	})

	return result
}

const arr = [1, 2, 3, 4, 5, 6, 2, 3, 4, 3, 4, 5, 5, 5]
console.log('getMaxCountByArr', getMaxCountByArr(arr))
